keep depth buffer from clearing

edited January 2014 in How To...

Is there a way to keep the depth buffer from clearing itself after a draw? If not - it would make sense, as in the 2d part of processing many of the art/effects are build upon not clearnig the color buffer and accumulating draw commands.

Tagged:

Answers

  • edited January 2014

    There was a way to, at least, access the depth buffer in Processing v1.5.1. Not sure if things have changed dramatically since then. I'll look into how I was doing it for this app: https://vimeo.com/63485316

  • It looks like that in Processing 2.0+ it is very tricky to access the depth buffer or to keep it from being cleared (both in the PApplet and in FrameBuffers) because there is a hard coded depth buffer clear before they allow rendering onto themselves.

    Processing 1.5.1 allows relatively better control of the depth buffer. For an example of how to do it download Processing 1.5.1 and check out this OpenProcessing sketch someone made using the .zbuffer of a PGraphics3D object: http://www.openprocessing.org/sketch/2950

    One note of interest I found was that in Processing 1.5.1 the "background(color)" command clears both the depth and the color buffer.

  • I actually just found a hack to disable depth buffer clearing! This is the hackiest dirtiest technique which is highly undocumented and unsupported.

    All of the depth clearing that occurs in processing that I could find does so by way of the
    PGL class's static int DEPTH_BUFFER_BIT. This variable is publicly assignable so

    Insert this- int oldBit = PGL.DEPTH_BUFFER_BIT; PGL.DEPTH_BUFFER_BIT = COLOR_BUFFER_BIT;

    anywhere you want depthbuffering to cease and processing's attempt to set the depthbuffer clear bit will fail.

    If you ever need to resume clearing use this in conjunction with the above: PGL.DEPTH_BUFFER_BIT = oldBit;

    Hope that helps!

  • This is a good observation. There is a hint called DISABLE_DEPTH_MASK than when is set it disables writing to the depth buffer... currently the background() function clears the depth buffer irrespective of the current status of this hint, but it shouldn't when it is set, what do you think?

  • Are 'hints' still supported? I no longer see them on the reference page. More to the point though, I presume that if you have disabled the depth mask you won't care terribly if it is cleared each frame. I suppose that there is the possibility of only disabling the depth buffer for part of the frame for something like a GUI. It still begs the question, why clear the depth buffer if writing to it is disabled? If the depth mask is 'disabled' it makes sense that it would be disabled everywhere, including in the background() function.

    I think depth-buffer effects are still a niche use case. While I, personally, would like to have more control (ie read & write control) over the depth buffer like there was in Processing 1.5.1, I don't think that it's current side effects should be changed. Perhaps, the 'background()' function could have a flagged version that would allow you choose what GL clear flags were set?

  • Hints are still supported. Although not mentioned in the standard reference, the hint() function is documented in the javadoc:

    http://processing.org/reference/javadoc/core/processing/core/PGraphics.html#hint(int)

    Adding an additional argument to background() is probably not an option. But from what you wrote, it seems you agree with not clearing the depth buffer in background() when the user disabled the depth mask through the hint() function. The code would look like:

    void setup() {
      size(200, 200, P3D); 
    }
    
    void draw() {
      hint(DISABLE_DEPTH_MASK);
      background(0);
      hint(ENABLE_DEPTH_MASK);
      ... 
    }
    

    I also opened an issue about this, https://github.com/processing/processing/issues/2296

  • thanks - it works perfectly!

    ps. if you set PGL.DEPTH_BUFFER_BIT = 0; then it wont clear the colorbuffer either

Sign In or Register to comment.